package com.ibm.law.ui.system.setup;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.beans.IBeanValueProperty;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.CellEditorProperties;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.databinding.viewers.ViewerProperties;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.ibm.law.domain.Deduction;
import com.ibm.law.domain.DeductionList;
import com.ibm.law.domain.Employee;
import com.ibm.law.domain.EmployeeList;
import com.ibm.law.domain.Salary;
import com.ibm.law.domain.SalaryList;
import com.ibm.law.service.system.DeductionService;
import com.ibm.law.service.system.EmployeeService;
import com.ibm.law.service.system.SalaryService;
import com.ibm.law.ui.common.CloseDialogSelectionAdapter;

public class SalarySetup extends Composite {
	private DataBindingContext m_bindingContext;
	private Table table;
	private Table table_1;
	private Table table_2;
	private TableViewer tableViewer_1;
	private TableViewer tableViewer_2;
	private Composite composite_3;
	private EmployeeList employeeList=new EmployeeList();
	private TableViewer tableViewer;
	private TableViewerColumn tableViewerColumn;
	private TableViewerColumn tableViewerColumn_1;
	private TableViewerColumn tableViewerColumn_4;
	private TableViewerColumn tableViewerColumn_2;
	private TableViewerColumn tableViewerColumn_3;
	private EmployeeService employeeService=new EmployeeService() ;
	private int type=1;
	private Employee employee;
	private SalaryList salaryList=new SalaryList();
	private SalaryService salaryService=new SalaryService();
	private DeductionList deductionList=new DeductionList();
	private DeductionService deductionService=new DeductionService();

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SalarySetup(Composite parent, int style) {
		
		super(parent, style);
		java.util.List<Employee> list=employeeService.getAll();
		System.out.println(list.get(0).getSalaries().size());
		System.out.println(list.get(0).getDeductions().size());
		
		employeeList.setList(employeeService.getAll());
		setLayout(new GridLayout(1, false));
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection=(IStructuredSelection)tableViewer.getSelection();
				if(selection!=null){
				employee=(Employee)selection.getFirstElement();
				salaryList.setList(salaryService.getSalariesByEmployee(employee));
				deductionList.setList(deductionService.getDeductionsByEmployee(employee));
				}
			}
		});
		table.setHeaderVisible(true);
		GridData gd_table = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_table.widthHint = 121;
		table.setLayoutData(gd_table);
		
		tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("姓名");
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		final StackLayout stackLayout = new StackLayout();
		Button btnRadioButton = new Button(composite_1, SWT.RADIO);
		btnRadioButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				stackLayout.topControl=tableViewer_1.getControl();
				type=1;
				composite_3.layout();
			}
		});
		btnRadioButton.setSelection(true);
		btnRadioButton.setText("应发");
		
		Button btnRadioButton_1 = new Button(composite_1, SWT.RADIO);
		btnRadioButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				stackLayout.topControl=tableViewer_2.getControl();
				type=2;
				composite_3.layout();
				
			}
		});
		btnRadioButton_1.setText("应扣");
		
		composite_3 = new Composite(composite_1, SWT.NONE);
		
		composite_3.setLayout(stackLayout);
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		
		tableViewer_1 = new TableViewer(composite_3, SWT.BORDER | SWT.FULL_SELECTION);
		table_1 = tableViewer_1.getTable();
		table_1.setBounds(0, 0, 85, 85);
		stackLayout.topControl=tableViewer_1.getControl();
		tableViewerColumn_1 = new TableViewerColumn(tableViewer_1, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setWidth(200);
		tblclmnNewColumn_1.setText("New Column");
		
		tableViewerColumn_4 = new TableViewerColumn(tableViewer_1, SWT.NONE);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("New Column");
		
		tableViewer_2 = new TableViewer(composite_3, SWT.BORDER | SWT.FULL_SELECTION);
		table_2 = tableViewer_2.getTable();
		
		tableViewerColumn_2 = new TableViewerColumn(tableViewer_2, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setWidth(200);
		tblclmnNewColumn_2.setText("New Column");
		
		tableViewerColumn_3 = new TableViewerColumn(tableViewer_2, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("New Column");
		
		Composite composite_2 = new Composite(this, SWT.NONE);
		composite_2.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_2.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		
		Button btnNewButton_6 = new Button(composite_2, SWT.NONE);
		btnNewButton_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Employee employee=new Employee();
				employeeList.add(employee);
				tableViewerColumn.getViewer().editElement(employee, 0);
				
			}
		});
		btnNewButton_6.setText("增加人员");
		
		Button btnNewButton_7 = new Button(composite_2, SWT.NONE);
		btnNewButton_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection selection =(IStructuredSelection)tableViewer.getSelection();
				if(selection!=null){
				Employee employee=(Employee)selection.getFirstElement();
				employeeService.update(employee);
				employeeService.delete(employee);
				tableViewer.getTable().setSelection(1);
				}
			}
		});
		btnNewButton_7.setText("删除人员");
		
		Button btnNewButton_8 = new Button(composite_2, SWT.NONE);
		btnNewButton_8.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				employeeService.saveAll(employeeList.getList());
			}
		});
		btnNewButton_8.setText("保存人员");
		
		Button btnNewButton_1 = new Button(composite_2, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(type==1){
					Salary salary=new Salary();
					salary.setEmployee(employee);
					salaryList.add(salary);
					tableViewerColumn_1.getViewer().editElement(salary, 0);
					
				}
				else{
					Deduction deduction=new Deduction();
					deduction.setEmployee(employee);
					deductionList.add(deduction);
					tableViewerColumn_2.getViewer().editElement(deduction, 0);
				}
			}
		});
		btnNewButton_1.setText("增加工资项");
		
		Button btnNewButton_2 = new Button(composite_2, SWT.NONE);
		btnNewButton_2.setText("删除工资项");
		
		Button btnNewButton_3 = new Button(composite_2, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				salaryService.saveAll(salaryList.getList());
				deductionService.saveAll(deductionList.getList());
			}
		});
		btnNewButton_3.setText("保存工资项");
		
		Button btnNewButton_5 = new Button(composite_2, SWT.NONE);
		btnNewButton_5.addSelectionListener(new CloseDialogSelectionAdapter());
		btnNewButton_5.setText("退出");
		m_bindingContext = initDataBindings();
		tableViewer.getTable().select(0);
		tableViewer.getTable().notifyListeners(SWT.Selection, null);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), Employee.class, "name");
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		tableViewer.setContentProvider(listContentProvider);
		//
		IObservableList listEmployeeListObserveList = BeanProperties.list("list").observe(employeeList);
		tableViewer.setInput(listEmployeeListObserveList);
		//
		CellEditor cellEditor = new TextCellEditor(tableViewer.getTable());
		IValueProperty cellEditorProperty = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty = BeanProperties.value("name");
		tableViewerColumn.setEditingSupport(ObservableValueEditingSupport.create(tableViewer, bindingContext, cellEditor, cellEditorProperty, valueProperty));
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider_1.getKnownElements(), Salary.class, new String[]{"name", "amount"});
		tableViewer_1.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		tableViewer_1.setContentProvider(listContentProvider_1);
		//
		IObservableList listSalaryListObserveList = BeanProperties.list("list").observe(salaryList);
		tableViewer_1.setInput(listSalaryListObserveList);
		//
		CellEditor cellEditor_1 = new TextCellEditor(tableViewer_1.getTable());
		IValueProperty cellEditorProperty_1 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_1 = BeanProperties.value("name");
		tableViewerColumn_1.setEditingSupport(ObservableValueEditingSupport.create(tableViewer_1, bindingContext, cellEditor_1, cellEditorProperty_1, valueProperty_1));
		//
		CellEditor cellEditor_2 = new TextCellEditor(tableViewer_1.getTable());
		IValueProperty cellEditorProperty_2 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_2 = BeanProperties.value("amount");
		tableViewerColumn_4.setEditingSupport(ObservableValueEditingSupport.create(tableViewer_1, bindingContext, cellEditor_2, cellEditorProperty_2, valueProperty_2));
		//
		ObservableListContentProvider listContentProvider_2 = new ObservableListContentProvider();
		IObservableMap[] observeMaps_1 = BeansObservables.observeMaps(listContentProvider_2.getKnownElements(), Deduction.class, new String[]{"name", "amount"});
		tableViewer_2.setLabelProvider(new ObservableMapLabelProvider(observeMaps_1));
		tableViewer_2.setContentProvider(listContentProvider_2);
		//
		IObservableList listDeductionListObserveList = BeanProperties.list("list").observe(deductionList);
		tableViewer_2.setInput(listDeductionListObserveList);
		//
		CellEditor cellEditor_3 = new TextCellEditor(tableViewer_2.getTable());
		IValueProperty cellEditorProperty_3 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_3 = BeanProperties.value("name");
		tableViewerColumn_2.setEditingSupport(ObservableValueEditingSupport.create(tableViewer_2, bindingContext, cellEditor_3, cellEditorProperty_3, valueProperty_3));
		//
		CellEditor cellEditor_4 = new TextCellEditor(tableViewer_2.getTable());
		IValueProperty cellEditorProperty_4 = CellEditorProperties.control().value(WidgetProperties.text(SWT.Modify));
		IBeanValueProperty valueProperty_4 = BeanProperties.value("amount");
		tableViewerColumn_3.setEditingSupport(ObservableValueEditingSupport.create(tableViewer_2, bindingContext, cellEditor_4, cellEditorProperty_4, valueProperty_4));
		//
		return bindingContext;
	}
}
